package cn.jdy.plugins.wap;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.PptList;
import cn.jdy.plugins.entity.Productx;
import cn.jdy.plugins.service.ProductxService;
 

/**
 * daihu
 * @title 产品控制类
 */
@Controller
@RequestMapping("/productx")
public class ProductxCtrl extends BaseCtrl {
  @Autowired
  private ProductxService productxService;
  @Autowired
  private ConfService confService;

  
 /**
  * 获取商品列表 
  * @param productx
  * @param key
  * @param pid
  * @return
  */
  @RequestMapping("")
  public ModelAndView list(Productx productx){

    ModelAndView mv = new ModelAndView("/productx/productxlist.jsp");
    productx.setStatus("0");//  
    productx.setRmtype("0");//  
    productx.setiDisplayStart(0);//  
    productx.setiDisplayLength(6);//  
    mv.addObject("dataList", productxService.query(productx)); //养老基地
    
    productx.setRmtype("1");//  
 
    mv.addObject("dataList1", productxService.query(productx));//热门预定
   // mv.addObject("pid",productx.getColumnId());
    return mv;
  }
  /**
	 * 分类首页
	 *
	 * @author: jga
	 * @date: 2018-10-15 16:53
	 */
	@RequestMapping("/indexfelei")
	public ModelAndView indexfelei(  ) {
		ModelAndView mv = new ModelAndView("/typeindex.jsp");
	//	try {
		//	Member member = super.getMember(true);
		//	mv.addObject("member", member);
		//} catch(Exception e) {
		//	mv.addObject("msg", "error");
			//BaseLog.e(this.getClass(), "updResetPwd:会员 资料管理有误", e);
	//	}
		return mv;
	}

  /**
   * 获取热门商品列表 
   * @param productx
   * @param key
   * @param pid
   * @return
   */
   @RequestMapping("cardlist")
   public ModelAndView cardlist(Productx productx){
     ModelAndView mv = new ModelAndView("/productx/cardlist.jsp");
     productx.setStatus("0");//  
     productx.setiDisplayStart(0);//  
     productx.setiDisplayLength(6);//  
     mv.addObject("dataList", productxService.query(productx)); //养老基地
     mv.addObject("rmtype",productx.getRmtype());
    // mv.addObject("pid",productx.getColumnId());
     return mv;
   }
   
  
  /**
	 * 加载ppt列表
	 * 
	 * @param rpSendId
	 * @return
	 */
	@RequestMapping("getproductxlist")
	@ResponseBody
	public Map<String, Object> getpptlist(int id, String rmtype,int pageNo, int pageSize) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		Long count = 0L;
		List<Productx> list = new ArrayList<Productx>();
		DetachedCriteria dc = DetachedCriteria.forClass(Productx.class);
		if (!StringUtil.isNullOrEmpty(id)) {
			dc.add(Restrictions.eq("mallId", id));
		}
		if (!StringUtil.isNullOrEmpty(rmtype)) {
			dc.add(Restrictions.eq("rmtype", rmtype));
		}
		dc.addOrder(Order.asc("createTime"));

		list = productxService.find(dc,pageNo, pageNo+pageSize);
		count = productxService.count(dc);
		jsonMap.put("datalist", list);
		jsonMap.put("iTotalDisplayRecords", count);
		return jsonMap;
	}
	
	
	
  /**
   * 异步获取商品列表
   * @param productx
   * @param key
   * @param pid
   * @return
   */
  @RequestMapping("/loadMore")
  @ResponseBody
  public Map<String, Object> loadMore(Productx productx){
    Map<String, Object> jsonMap = new HashMap<String, Object>();
    try {
      productx.setStatus("0");// 状态 0已发布、1已下架、2审核中、3审核不通过
      jsonMap.put("dataList", productxService.querypage(productx));
      jsonMap.put("iTotalDisplayRecords", productxService.count(productx));
    } catch(Exception e) {
      BaseLog.e(this.getClass(), "查询异常", e);
    }
    return jsonMap;
  }
  
  /**
   * 产品详情
   * @param id
   * @return
   */
  @RequestMapping("/show-{id}.html")
  public ModelAndView queryDetail(@PathVariable Long id) {
    ModelAndView mv = new ModelAndView("/productx/productx.jsp");
    mv.addObject("data", id==null||id==0 ? id : this.productxService.find(id));
    return mv;
  }
  
}
